﻿using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ITViet.Domains;
using ITViet.Services;
using ITViet.Web.App_Code;

namespace ITViet.Web.Pages
{
	public partial class Home : BasePage
	{
		#region Events
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack && !IsCallback)
			{
				LoadIntro();
				LoadHotProducts();
				LoadHotNews();
			}
		}
		#endregion

		#region Methods
		private void LoadIntro()
		{
			TList<HtmlContent> htmlPurchase = new HtmlContentService().GetByTypeAndLanguage((int)MenuTypeList.PurchaseService, CurrentLanguage.Id);
			if (htmlPurchase.Count > 0)
			{
				ltPurchaseService.Text = htmlPurchase[0].Summary;
			}

			TList<HtmlContent> htmlRepair = new HtmlContentService().GetByTypeAndLanguage((int)MenuTypeList.PurchaseService, CurrentLanguage.Id);
			if (htmlRepair.Count > 0)
			{
				ltRepairService.Text = htmlRepair[0].Summary;
			}

			TList<HtmlContent> htmlWarranty = new HtmlContentService().GetByTypeAndLanguage((int)MenuTypeList.PurchaseService, CurrentLanguage.Id);
			if (htmlWarranty.Count > 0)
			{
				ltWarrantyService.Text = htmlWarranty[0].Summary;
			}
		}

		private void LoadHotProducts()
		{
			rptHotProduct.DataSource = new ProductLocaleService().GetTopHot(CurrentLanguage.Id);
			rptHotProduct.DataBind();
		}

		private void LoadHotNews()
		{
			rptHotNews.DataSource = new NewsService().GetByHomeSearch(null, CurrentLanguage.Id, null, true, NewsCategoryType.Normal);
			rptHotNews.DataBind();
		}

		public string TextToHtml(string text, int length)
		{
			string result = string.Empty;
			if (!string.IsNullOrEmpty(text))
			{
				if (text.Length > length)
				{
					result = text.Substring(0, length) + "...";
				}
				else
				{
					result = text;
				}

				result = result.Replace("\n", "<br>");
			}

			return result;
		}
		#endregion
	}
}
